package qingke.struts.example.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import ognl.DefaultTypeConverter;

public class MyDateTypeConverter extends DefaultTypeConverter{
	
	private static String format="yyyy/MM/dd";
	
	public MyDateTypeConverter(){
		
	}	
	@Override
	public Object convertValue(Map context, Object value, Class toType) {
		
		if(toType==Date.class){
			System.out.println("start to convert from string to date");
			SimpleDateFormat sdf=new SimpleDateFormat(format);
			try {
				return sdf.parse(((String[])value)[0]);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else if(toType==String.class){
		   System.out.println("start to convert from date to string");
		   SimpleDateFormat sdf=new SimpleDateFormat(format);
		   return sdf.format((Date)value);
		}
		System.out.println("end of convert");
		
		return null;
	}

}
